package red.mlz.module.module.gametagrelation.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import red.mlz.module.module.gametagrelation.entity.GameTagRelation;
import red.mlz.module.module.gametagrelation.mapper.GameTagRelationMapper;
import red.mlz.module.utils.BaseUtils;

import java.util.List;

@Service
public class GameTagRelationService {

    @Autowired
    private GameTagRelationMapper gameTagRelationMapper;

    public List<GameTagRelation> findByGameId(Long gameId) {
        return gameTagRelationMapper.findByGameId(gameId);
    }

    public GameTagRelation getById(Long gameTagRelationId) {

        return gameTagRelationMapper.getById(gameTagRelationId);
    }

    public GameTagRelation extractById(Long gameTagRelationId) {

    return gameTagRelationMapper.extractById(gameTagRelationId);
    }

    public Integer insert(GameTagRelation entity) {
        // 检查是否存在已删除的相同关系
        GameTagRelation deletedRelation = gameTagRelationMapper.getDeletedByGameIdAndTagId(
            entity.getGameId(), entity.getTagId());

        if (deletedRelation != null) {
            // 恢复已删除的关系
            deletedRelation.setIsDeleted(0);
            deletedRelation.setUpdateTime(entity.getUpdateTime());
            return gameTagRelationMapper.update(deletedRelation);
        }

        return gameTagRelationMapper.insert(entity);
    }

    public Integer update(GameTagRelation entity) {

    return gameTagRelationMapper.update(entity);
    }

    public Integer delete(Long id, Integer updateTime) {

    return gameTagRelationMapper.delete(updateTime, id);
    }

    public GameTagRelation getByGameIdAndTagId(Long gameId, Long tagId) {
        return gameTagRelationMapper.getByGameIdAndTagId(gameId, tagId);
    }

    public List<GameTagRelation> getByGameId(Long gameId) {
        return gameTagRelationMapper.getByGameId(gameId);
    }

    public void deleteByGameId(Long gameId) {
        gameTagRelationMapper.deleteByGameId(BaseUtils.currentSeconds(), gameId);
    }


}